<?php
/**
 * Created by PhpStorm.
 * User: zhana0
 * Date: 18-3-11
 * Time: 下午10:39
 */

namespace App\Http\Controllers;
use App\Model\Friends;
use App\Model\Tokens;
use App\Model\Users;
use Illuminate\Routing\Controller as BaseController;

class UserController extends BaseController
{
    private $user;

    public function __construct()
    {
        $this->user = new Users();
    }

    /**
     * 登录
     * @return \Illuminate\Http\JsonResponse
     */
    public function login()
    {
        try{
            $user_name = request('user_name');
            $password = request('password');
            $rel = $this->user->where(['user_name'=>$user_name,'password'=>md5($password)])->first();
            if ($rel){
                $token = Tokens::refresh_token($rel->id);
                return response()->json(['code'=>1,'token'=>$token]);
            } else {
                return response()->json(['code'=>0,'msg'=>'用户名或密码错误']);
            }
        }catch (\Exception $e)
        {
            return response()->json(['code'=>0,'msg'=>'查询错误;' . $e->getMessage()]);
        }
    }

    /**
     * 注册
     * @return \Illuminate\Http\JsonResponse
     */
    public function reg()
    {
        try{
            $user_name = request('user_name');
            $password = request('password');
            $bool = $this->user->where(['user_name'=>$user_name])->first();
            if ($bool) {
                throw new \Exception('用户名重复');
            }
            $rel = $this->user->reg(['user_name'=>$user_name,'password'=>md5($password)]);
            dd($rel);
            if ($rel) {
                return response()->json(['code'=>1,'token'=>$rel['token']]);
            } else {
                return response()->json(['code'=>0,'msg'=>'注册失败']);
            }
        } catch (\Exception $e)
        {
            return response()->json(['code'=>0,'msg'=>'注册失败,'.$e->getMessage()]);
        }
    }

    /**
     * 添加好友
     * @return \Illuminate\Http\JsonResponse
     */
    public function add_friend()
    {
        try{
            $friend_name = request('friend_name');
            $token = request('token');
            if ($token = Tokens::verify_token($token)){
                $my = $this->user->where(['id'=>$token->user_id])->first();
                $friend = $this->user->where(['user_name'=>$friend_name])->first();
            } else {
                throw new \Exception('token验证失败');
            }

            if ($my && $friend) {
                Friends::create(['user_id'=>$my->id,'friend_id'=>$friend->id]);
                return response()->json(['code'=>0,'msg'=>'添加成功']);
            } else {
                throw new \Exception('用户不存在');
            }

        } catch (\Exception $e)
        {
            return response()->json(['code'=>0,'msg'=>$e->getMessage()]);
        }

    }

    /**
     * 获取好友列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function get_friends()
    {
        try{
            $token = request('token');
            if ($token = Tokens::verify_token($token)){
                $rel = $this->user->where(['id'=>$token->user_id])->with('friends')->first();
                return response()->json(['code'=>1,'data'=>$rel->friends]);
            } else {
                throw new \Exception('token验证失败');
            }
        } catch (\Exception $exception)
        {
            return response()->json(['code'=>0,'msg'=>$exception->getMessage()]);
        }

    }

    /**
     * 获取用户名称
     * @return \Illuminate\Http\JsonResponse
     */
    public function get_user_name()
    {
        try {
            $token = request('token');
            $user_ids = request('user_ids');
            if (!Tokens::verify_token($token)) {
                throw new \Exception('token验证失败');
            }

            $user_name = $this->user->find($user_ids);
            return response()->json(['code'=>1,'data'=>$user_name]);

        } catch (\Exception $exception)
        {
            return response()->json(['code'=>0,'msg'=>$exception->getMessage()]);
        }
    }
}